package com.redteam.models;

import com.redteam.data.beans.Airplane;
import com.redteam.data.dao.AirplaneDAO;
import com.redteam.data.dao.interfaces.DAO;
import java.sql.SQLException;
import java.sql.Date;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Communicates with the AirplaneDAO
 * @author Franco
 */
public class AirplaneModifier {

    DAO dao = AirplaneDAO.getInstance();

    public int removePlane(int key) {
        try {
            return dao.remove(key);
        } catch (SQLException ex) {
            Logger.getLogger(AirplaneModifier.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }

    public int updatePlane(Map<String, String[]> planes) {
        String[] planeInfo = (String[]) planes.get("plane");
        try {
            Airplane plane = (Airplane) dao.getSingle(Integer.parseInt(planes.get("planeID")[0]));
            plane.setFuelGrade(planeInfo[0]);
            plane.setCurrentFuel(Float.parseFloat(planeInfo[1]));
            plane.setUsableFuel(Float.parseFloat(planeInfo[2]));
            plane.setOilGrade(planeInfo[3]);
            plane.setOilCapacity(Float.parseFloat(planeInfo[4]));
            plane.setCurrentOil(Float.parseFloat(planeInfo[5]));
            plane.setHobbsLeftEngine(Float.parseFloat(planeInfo[6]));
            plane.setHobbsLastOverhaul(Float.parseFloat(planeInfo[7]));
            plane.setDateLastOverhaul(Date.valueOf(planeInfo[8]));
            plane.setAnnualDate(Date.valueOf(planeInfo[9]));
            plane.setHobbsLastHundred(Float.parseFloat(planeInfo[10]));
            plane.setDateLastHundred(Date.valueOf(planeInfo[11]));
            return dao.update(plane);
        } catch (SQLException ex) {
            Logger.getLogger(AirplaneModifier.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }

    public int insertPlane(Map<String, String[]> newplane) {
        try {
            Airplane plane = new Airplane();
            plane.setModel(newplane.get("model")[0]);
            plane.setManufacturer(newplane.get("manufacturer")[0]);
            plane.setPlaneType(newplane.get("planeType")[0]);
            plane.setFuelGrade(newplane.get("fuelGrade")[0]);
            plane.setCurrentFuel(Float.parseFloat(newplane.get("currentFuel")[0]));
            plane.setUsableFuel(Float.parseFloat(newplane.get("usableFuel")[0]));
            plane.setOilGrade(newplane.get("oilGrade")[0]);
            plane.setOilCapacity(Float.parseFloat(newplane.get("oilCapacity")[0]));
            plane.setCurrentOil(Float.parseFloat(newplane.get("currentOil")[0]));
            plane.setHobbsLeftEngine(Float.parseFloat(newplane.get("hobbsLeftEngine")[0]));
            plane.setHobbsLastOverhaul(Float.parseFloat(newplane.get("hobbsLastOverhaul")[0]));
            plane.setDateLastOverhaul(Date.valueOf(newplane.get("dateLastOverhaul")[0]));
            plane.setAnnualDate(Date.valueOf(newplane.get("annualDate")[0]));
            plane.setHobbsLastHundred(Float.parseFloat(newplane.get("hobbsLastHundred")[0]));
            plane.setDateLastHundred(Date.valueOf(newplane.get("dateLastHundred")[0]));
            return dao.insert(plane);
        } catch (SQLException ex) {
            Logger.getLogger(AirplaneModifier.class.getName()).log(Level.SEVERE, null, ex);
        }
        return 0;
    }

    public List<Airplane> getAllPlanes() {
        List<Airplane> planes = null;
        try {
            planes = (List) dao.getAll();
        } catch (SQLException ex) {
            Logger.getLogger(AirplaneModifier.class.getName()).log(Level.SEVERE, null, ex);
        }
        return planes;
    }
}
